<?php
/**
 * $Id:$
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

if (file_exists('../config.php')) {
	header('Content-Type: text/plain');
	die('fgs-backend is still installed. Please remove config.php in top directory before install.');
}

// Includes
require_once('../api/fgs.php');
require_once('../scripts/class/html_form.class.php');

fgs::init(false);

// Variables
$errors = array();
$form = new htmlForm('install');
$infos = array();

// Install ?
if ($form->isPost()) {
	$db_host = $form->get('db_host');
	$db_base = $form->get('db_base');
	$db_user = $form->get('db_user');
	$db_pass = $form->get('db_pass');
	$db_create = $form->get('db_create');
	$db_drop_table = $form->get('db_drop_table');
	$admin_pass = $form->get('admin_pass');
	$admin_pass2 = $form->get('admin_pass2');
	$url_type_clean = $form->get('url_clean');

	if (empty($db_host))
		$errors[] = 'Database host is empty';
	if (empty($db_base))
		$errors[] = 'No base specified';
	else if (!eregi('^([a-zA-Z0-9_\-]+)$', $db_base))
		$errors[] = 'Invalid base name';
	if (empty($db_user))
		$errors[] = 'No user specified';

	if (empty($admin_pass))
		$errors[] = 'No admin password specified';
	else if ($admin_pass != $admin_pass2)
		$errors[] = 'Admin password doesn\'t match';

	if (!count($errors)) {
		$sql = new fgsSql($db_host, null, $db_user, $db_pass);
		if ($sql->isError())
			$errors[] = $sql->error();

		// create database
		if (!count($errors)) {
			if (!empty($db_create)) {
				if (!$sql->createDatabase($db_base))
					$errors[] = $sql->error();
			} else if (!$sql->database($db_base)) {
				$errors[] = $sql->error();
			}
		}
		
		// import tables
		if (!count($errors)) {
			if (!$sql->importXML('fgs-create.xml', $db_drop_table))
					$errors[] = $sql->error();
		}

		// write config
		if (!count($errors)) {
			$config = & fgs::config();
			$config['db_host'] = $db_host;
			$config['db_base'] = $db_base;
			$config['db_user'] = $db_user;
			$config['db_pass'] = $db_pass;
			$config['admin_pass'] = $admin_pass;
			$config['url_type'] = $url_type_clean ? 'clean' : 'normal';
      $config['url_master_server'] = 'http://freegs.net/remote/RPC2';

			$infos[] = sprintf('
				<h2>Configuration for ../config.php</h2>
				<pre class="config">%s</pre>', 
				htmlentities(fgs::showConfig())
			);
		}

	}

}

// Prepare form
$form->addSection('Database');
$form->addText('Please indicate all information about database to use');
$form->addField('* Host', 'db_host', 'text');
$form->addField('* Database', 'db_base', 'text');
$form->addField('* User', 'db_user', 'text');
$form->addField('Password', 'db_pass', 'password');
$form->addField('Create database', 'db_create', 'checkbox');
$form->addField('Drop table if exist', 'db_drop_table', 'checkbox');

$form->addSection('Url');
$form->addText('You can use "clean url" method, but not work on all apache installation');
$form->addField('Use "clean url"', 'url_clean', 'checkbox');

$form->addSection('Administration');
$form->addText('Please indicate a password for administration panel');
$form->addField('* Password', 'admin_pass', 'password');
$form->addField('* Password (repeat)', 'admin_pass2', 'password');

$form->addSubmit();



// Output
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="../styles/fgs.css"/>
	<title>fgs-backend installation</title>
</head>
<body>
<div id="wrapper">
	<div id="top">
		<h1>fgs-backend installation</h1>
	</div>
	<div id="content">
	<?
		if (count($errors)) {
			printf('<div class="error">They are some errors : <pre>%s</pre></div>',
				implode("\n", $errors)
			);
		}
		if (count($infos)) {
			printf('<div class="infos">%s</div>',
				implode("\n", $infos)
			);
		}
		$form->output();
	?>
	</div>
	<div id="sidecontent">&nbsp;</div>
	 
	<div id="footer">
		<span class="left">&copy; 2006 <a href="http://freegs.net">freegs.net</a></span>
		<span class="right">	
		Designed by <a href="http://www.oswd.org/user/profile/id/8671">quixotic</a>
		</span>
	</div>
</div>
</body>
</html>
